package com.rui.study.micro.usermanager.controller;

import com.rui.study.micro.usermanager.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    private static final String SIGN_UP_FAILED_TIPS = "Sign up failed";
    private static final String SIGN_IN_FAILED_TIPS = "Sign in failed";


    @Autowired
    private UserService userService;



    /**
     * 用户注册--页面
     * @return
     */
    @RequestMapping(value = "/sign_up_page", method = RequestMethod.GET)
    public String signUpPage() {
        return "signUp";
    }

//    /**
//     * 用户注册
//     * @param userInfoDTO
//     * @param bindingResult
//     * @return
//     */
//    @RequestMapping(value = "/sign_up", method = RequestMethod.POST)
//    @ResponseBody
//    public Result signUp(HttpSession session,
//                         @Validated(SignUp.class) @RequestBody UserInfoDTO userInfoDTO,
//                         BindingResult bindingResult) {
//        // 数据转换
//        UserInfo userInfo = dtoConvert(userInfoDTO, UserInfo.class);
//        userInfo.setPassword(MD5Util.encrypt(userInfo.getPassword())); // 对密码进行MD5加密
//        // 创建用户
//        userService.createUserInfo(userInfo);
//        // 移除token
//        removeSubmitToken(session);
//        LOGGER.info("sign up succeed");
//        return new Result();
//    }

    /**
     * 用户登陆--页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/sign_in_page", method = RequestMethod.GET)
    public String signInPage(HttpServletRequest request) {
        return "signIn";
    }

//    /**
//     * 用户登录
//     * @param request
//     * @param userInfoDTO
//     * @param bindingResult
//     * @return
//     */
//    @RequestMapping(value = "/sign_in", method = RequestMethod.POST)
//    @ResponseBody
//    public Result signIn(HttpServletRequest request,
//                         @RequestBody @Validated({SignIn.class}) UserInfoDTO userInfoDTO,
//                         BindingResult bindingResult) {
//        String username = userInfoDTO.getUsername().toString();
//        String password = MD5Util.encrypt(userInfoDTO.getPassword());
//        // 登录
//        Subject subject = SecurityUtils.getSubject();
//        printSessionId(subject, "sign_IN");
//        subject.login(new UsernamePasswordToken(username, password));
//        // 移除token
//        removeSubmitToken(request.getSession());
//        LOGGER.info("Sign in succeed: [{}]", username);
//        return new Result();
//    }


//    @RequestMapping(value = "/sign_out", method = RequestMethod.GET)
//    public void signOut() {
//        Subject subject = SecurityUtils.getSubject();
//        printSessionId(subject, "sign_OUT");
//        subject.logout();
//    }

}
